package com.nandi.deviceinstall.presenter

import com.nandi.deviceinstall.base.BasePresenter
import com.nandi.deviceinstall.bean.*
import com.nandi.deviceinstall.contract.BindRecordContract
import com.nandi.deviceinstall.model.BindRecordModel
import com.nandi.deviceinstall.net.BaseSubscriber
import com.nandi.deviceinstall.net.RxScheduler
import com.uber.autodispose.FlowableSubscribeProxy
import org.reactivestreams.Subscription

class BindRecordPresenter : BasePresenter<BindRecordContract.View>(), BindRecordContract.Presenter {
    override fun requestDeleteBindRecord(token: String, id: Int) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeDeleteBindRecord(token,id)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<BindDeleteBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<BindDeleteBean>(mView!!) {
                    override fun onNext(t: BindDeleteBean) {
                        mView!!.handlerDeleteBind(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListDevFactory(token: String,factory: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListDevFactory(token,factory)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DeviceFacBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DeviceFacBean>(mView!!) {
                    override fun onNext(t: DeviceFacBean) {
                        mView!!.handlerDevFactory(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }


    private val model: BindRecordContract.Model

    init {
        model = BindRecordModel()
    }

    override fun requestDisNum(token: String, disName: String, areaCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeDisNum(token, disName, areaCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisNumBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisNumBean>(mView!!) {
                    override fun onNext(t: DisNumBean) {
                        mView!!.handlerDisNum(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListDisaster(token: String, current: Int, size: Int, areaCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListDisaster(token, current, size, areaCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisasterBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisasterBean>(mView!!) {
                    override fun onNext(t: DisasterBean) {
                        mView!!.handlerListDisasterResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListTown(token: String, id: Int) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListTown(token, id)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<TownBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<TownBean>(mView!!) {
                    override fun onNext(t: TownBean) {
                        mView!!.handlerListTownResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListCity(token: String, district: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListCity(token, district)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<CityBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<CityBean>(mView!!) {
                    override fun onNext(t: CityBean) {
                        mView!!.handlerListCityResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListRecord(token: String, current: Int, size: Int, areaCode: String, sort: Int,
                                   townCode: String, disasterName: String, disNo: String, monitorNo: String,
                                   startTime: String, endTime: String, isAppAdd: Int, devType: Int,
                                   devName: String, devFactory: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListRecord(token, current, size, areaCode, sort, townCode, disasterName,
                disNo, monitorNo, startTime, endTime, isAppAdd, devType, devName, devFactory)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<BindRecordBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<BindRecordBean>(mView!!) {
                    override fun onNext(t: BindRecordBean) {
                        mView!!.handlerListRecordResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestListMonitorNum(token: String, unifiedCode: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.getListMonitorNum(token,unifiedCode)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<ListMonitorNumBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<ListMonitorNumBean>(mView!!) {
                    override fun onNext(t: ListMonitorNumBean) {
                        mView!!.handlerMonitorNum(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }

    override fun requestDisName(token: String, disasterName: String, town: String) {
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeListDisName(token, disasterName,town)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<DisNameBean>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<DisNameBean>(mView!!) {
                    override fun onNext(t: DisNameBean) {
                        mView!!.handlerDisNameResult(t)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }
}